package view;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JPanel;

import layout.TableLayout;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import com.hexidec.util.Translatrix;

import eventHandling.EventBus;

/**
 * A StatisticsPanel displays information about the lesson. The number of cards
 * in one batch, how many expired cards, how many lerarned and how many
 * unlearned cards.
 * 
 * @author sloth42
 * 
 */
public class StatisticsPanel extends JPanel {

	private DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	private EventBus eventBus;
	private JFreeChart chart;
	private ChartPanel chartPanel;

	public StatisticsPanel(EventBus eventBus) {
		super();
		this.eventBus = eventBus;
		double border = 5;
		double[][] size = { { border, TableLayout.FILL, border },
				{ border, TableLayout.FILL, border } };
		this.setLayout(new TableLayout(size));
		this.setBackground(Color.WHITE);
		this.setMinimumSize(new Dimension(400, 200));

		initChartPanel();

		this.add(chartPanel, "1,1");

	}

	private void initChartPanel() {
		chart = ChartFactory.createStackedBarChart(null, null, null, dataset,
				PlotOrientation.VERTICAL, false, true, false);

		CategoryPlot plot = chart.getCategoryPlot();
		plot.getRangeAxis().setVisible(false);
		BarRenderer renderer = (BarRenderer) plot.getRenderer();
		renderer.setDrawBarOutline(true);
		renderer.setMaximumBarWidth(0.1);

		// renderer.set
		chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(450, 240));
		// disable zoom
		chartPanel.setRangeZoomable(false);
		// disable menu
		chartPanel.setPopupMenu(null);

	}

	/**
	 * This methode adds a bar to the statistics panel which represents a
	 * longterm batch.
	 * 
	 * @param batchName
	 *            name of the batch
	 * @param numberOfLearnedCards
	 *            number of unlearned cards
	 * @param numberOfExpiredCards
	 *            number of expired cards
	 */
	public void addLongTermBar(String batchName, int numberOfLearnedCards,
			int numberOfExpiredCards) {
		dataset.addValue(numberOfLearnedCards, Translatrix
				.getTranslationString("learnedCards"), batchName);
		dataset.addValue(numberOfExpiredCards, Translatrix
				.getTranslationString("expiredCards"), batchName);
	}

	// FIXME javadoc
	/**
	 * this method adds a bar to the statistics panel.
	 * 
	 * @param batchName
	 * @param numberOfLearnedCards
	 * @param numberOfExpiredCards
	 * @param numberOfUnlearnedCards
	 */
	public void addBar(String batchName, int numberOfLearnedCards,
			int numberOfExpiredCards, int numberOfUnlearnedCards) {

		dataset.addValue(numberOfExpiredCards, Translatrix
				.getTranslationString("expiredCards"), batchName);
		dataset.addValue(numberOfLearnedCards, Translatrix
				.getTranslationString("learnedCards"), batchName);
		dataset.addValue(numberOfUnlearnedCards, Translatrix
				.getTranslationString("unlearnedCards"), batchName);

	}

	public void updateOverallBar(int numberOfExpiredCards,
			int numberOfUnLearnedCards, int numberOfLearnedCards) {
		//TODO fix name of batch ("test")
		dataset.setValue(numberOfExpiredCards, Translatrix
				.getTranslationString("unlearnedCards"), "test");

	}

}